namespace SmartSoft.Invoices
{
    using SmartSoft;
    using SmartSoft.Controls;
    using SmartSoft.Controls.ChildGridControls;
    using SmartSoft.Properties;
    using SmartSoft.ADP;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormFACalculate : FormControl
    {
        private BtnControl btnCalculate;
        private BtnControl btnCancel;
        private ToolStripButton btnClose;
        private BtnControl btnDepreciateGet;
        private BtnControl btnMake;
        private BtnControl btnSave;
        private DataGridViewTextBoxColumn Column1;
        private DataGridViewTextBoxColumn Column10;
        private DataGridViewTextBoxColumn Column2;
        private DataGridViewTextBoxColumn Column3;
        private OptionColumn Column4;
        private NumericColumn Column5;
        private NumericColumn Column6;
        private NumericColumn Column7;
        private NumericColumn Column8;
        private NumericColumn Column9;
        private IContainer components;
        private GridControl grdControl;
        private LabelControl lblMonth;
        private DataTable oFixedAssetTable;
        private DmTsDataAccess oMethod = new DmTsDataAccess();
        private ToolStripSeparator tbrSep01;
        private ToolStrip Toolbars;
        private TextNumeric txtMonth;
        private TbarSearch txtSearch;
        private TextNumeric txtYear;

        public FormFACalculate()
        {
            this.InitializeComponent();
            this.grdControl.AutoGenerateColumns = false;
            this.txtMonth.Value = AppInstance.EndWorkingDate.Month;
            this.txtYear.Value = AppInstance.EndWorkingDate.Year;
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            DateTime time = new DateTime((int) this.txtYear.NumericValue, (int) this.txtMonth.NumericValue, 1);
            DateTime time2 = time.AddMonths(1).AddDays(-1.0);
            if ((time2 < AppInstance.OpeningDateOfSubEnterprice) || (time2 <= AppInstance.EndingDateOfSubEnterprice))
            {
                MessageBox.Show("Đã khóa dữ liệu!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.oFixedAssetTable = this.oMethod.Depreciation(AppInstance.Subsidiary, time, time2);
                this.grdControl.DataSource = new BindingSource(this.oFixedAssetTable, this.oFixedAssetTable.TableName);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.GetDepreciation();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnDepreciateGet_Click(object sender, EventArgs e)
        {
            this.GetDepreciation();
        }

        private void btnMake_Click(object sender, EventArgs e)
        {
            DateTime time2 = new DateTime((int) this.txtYear.NumericValue, (int) this.txtMonth.NumericValue, 1);
            DateTime time = time2.AddMonths(1).AddDays(-1.0);
            if ((time < AppInstance.OpeningDateOfSubEnterprice) || (time <= AppInstance.EndingDateOfSubEnterprice))
            {
                MessageBox.Show("Đã khóa dữ liệu!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                new FormCTKTEditor("PKTS", time, this.oFixedAssetTable).Show();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DateTime time = new DateTime((int) this.txtYear.NumericValue, (int) this.txtMonth.NumericValue, 1);
            DateTime time2 = time.AddMonths(1).AddDays(-1.0);
            if ((time2 < AppInstance.OpeningDateOfSubEnterprice) || (time2 <= AppInstance.EndingDateOfSubEnterprice))
            {
                MessageBox.Show("Đã khóa dữ liệu!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.oMethod.Save4(AppInstance.Subsidiary, time, time2, this.oFixedAssetTable);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void GetDepreciation()
        {
            DateTime time = new DateTime((int) this.txtYear.NumericValue, (int) this.txtMonth.NumericValue, 1);
            DateTime time2 = time.AddMonths(1).AddDays(-1.0);
            this.oFixedAssetTable = this.oMethod.GetDepreciation(AppInstance.Subsidiary, time, time2);
            this.grdControl.DataSource = new BindingSource(this.oFixedAssetTable, this.oFixedAssetTable.TableName);
        }

        private void grdControl_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.ColumnIndex >= 4) && (e.ColumnIndex <= 7))
            {
                DataRow row = ((DataRowView) this.grdControl.CurrentRow.DataBoundItem).Row;
                row["Tien2"] = ((((decimal) row["Tien2_N1"]) + ((decimal) row["Tien2_N2"])) + ((decimal) row["Tien2_N3"])) + ((decimal) row["Tien2_N4"]);
                row.EndEdit();
            }
        }

        private void grdControl_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.ColumnIndex >= 4) && (e.ColumnIndex <= 7))
            {
                DataRow row = ((DataRowView) this.grdControl.CurrentRow.DataBoundItem).Row;
                row.EndEdit();
                if (row["Edited"].ToString() == "C")
                {
                    this.grdControl[4, this.grdControl.CurrentCell.RowIndex].ReadOnly = false;
                    this.grdControl[5, this.grdControl.CurrentCell.RowIndex].ReadOnly = false;
                    this.grdControl[6, this.grdControl.CurrentCell.RowIndex].ReadOnly = false;
                    this.grdControl[7, this.grdControl.CurrentCell.RowIndex].ReadOnly = false;
                }
                else
                {
                    this.grdControl[4, this.grdControl.CurrentCell.RowIndex].ReadOnly = true;
                    this.grdControl[5, this.grdControl.CurrentCell.RowIndex].ReadOnly = true;
                    this.grdControl[6, this.grdControl.CurrentCell.RowIndex].ReadOnly = true;
                    this.grdControl[7, this.grdControl.CurrentCell.RowIndex].ReadOnly = true;
                }
            }
        }

        private void InitializeComponent()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            DataGridViewCellStyle style2 = new DataGridViewCellStyle();
            DataGridViewCellStyle style3 = new DataGridViewCellStyle();
            DataGridViewCellStyle style4 = new DataGridViewCellStyle();
            DataGridViewCellStyle style5 = new DataGridViewCellStyle();
            DataGridViewCellStyle style6 = new DataGridViewCellStyle();
            //ComponentResourceManager manager = new ComponentResourceManager(typeof(FormFACalculate));
            this.grdControl = new GridControl();
            this.Column1 = new DataGridViewTextBoxColumn();
            this.Column2 = new DataGridViewTextBoxColumn();
            this.Column3 = new DataGridViewTextBoxColumn();
            this.Column4 = new OptionColumn();
            this.Column5 = new NumericColumn();
            this.Column6 = new NumericColumn();
            this.Column7 = new NumericColumn();
            this.Column8 = new NumericColumn();
            this.Column9 = new NumericColumn();
            this.Column10 = new DataGridViewTextBoxColumn();
            this.lblMonth = new LabelControl();
            this.Toolbars = new ToolStrip();
            this.txtSearch = new TbarSearch();
            this.tbrSep01 = new ToolStripSeparator();
            this.btnClose = new ToolStripButton();
            this.txtMonth = new TextNumeric();
            this.txtYear = new TextNumeric();
            this.btnCalculate = new BtnControl();
            this.btnDepreciateGet = new BtnControl();
            this.btnSave = new BtnControl();
            this.btnCancel = new BtnControl();
            this.btnMake = new BtnControl();
            ((ISupportInitialize) this.grdControl).BeginInit();
            this.Toolbars.SuspendLayout();
            base.SuspendLayout();
            this.grdControl.AllowUserToAddRows = false;
            this.grdControl.AllowUserToDeleteRows = false;
            this.grdControl.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.grdControl.CheckAutoSize = true;
            this.grdControl.CheckColor = false;
            this.grdControl.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.grdControl.Columns.AddRange(new DataGridViewColumn[] { this.Column1, this.Column2, this.Column3, this.Column4, this.Column5, this.Column6, this.Column7, this.Column8, this.Column9, this.Column10 });
            this.grdControl.Font = new Font("Arial", 8.25f);
            this.grdControl.Location = new Point(0, 0x38);
            this.grdControl.Name = "grdControl";
            this.grdControl.Size = new Size(0x31a, 0x1dc);
            this.grdControl.TabIndex = 7;
            this.grdControl.CellEndEdit += new DataGridViewCellEventHandler(this.grdControl_CellEndEdit);
            this.grdControl.CellEnter += new DataGridViewCellEventHandler(this.grdControl_CellEnter);
            this.Column1.DataPropertyName = "Ma_Ts";
            this.Column1.HeaderText = "Mã tài sản";
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            this.Column1.Width = 60;
            this.Column2.DataPropertyName = "Ten_Ts";
            this.Column2.HeaderText = "Tên tài sản";
            this.Column2.Name = "Column2";
            this.Column2.ReadOnly = true;
            this.Column2.Width = 0xb3;
            this.Column3.DataPropertyName = "Ma_Bp";
            this.Column3.HeaderText = "Bộ phận";
            this.Column3.Name = "Column3";
            this.Column3.ReadOnly = true;
            this.Column3.Width = 60;
            this.Column4.DataPropertyName = "Edited";
            style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.Column4.DefaultCellStyle = style;
            this.Column4.DisabledForeColor = Color.Blue;
            this.Column4.HeaderText = "Sửa";
            this.Column4.InputMask = "C,K";
            this.Column4.Name = "Column4";
            this.Column4.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column4.UserTextLength = AppInstance.TextControlLength.None;
            this.Column4.Width = 30;
            this.Column5.DataPropertyName = "Tien2_N1";
            style2.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column5.DefaultCellStyle = style2;
            this.Column5.HeaderText = "ngân s\x00e1ch";
            this.Column5.Name = "Column5";
            this.Column5.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column5.UserTextNumericFormat = AppInstance.TextNumericFormat.LocalAmount;
            this.Column5.Width = 70;
            this.Column5.ZeroIsValid = true;
            this.Column6.DataPropertyName = "Tien2_N2";
            style3.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column6.DefaultCellStyle = style3;
            this.Column6.HeaderText = "Tự bổ sung";
            this.Column6.Name = "Column6";
            this.Column6.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column6.UserTextNumericFormat = AppInstance.TextNumericFormat.LocalAmount;
            this.Column6.Width = 70;
            this.Column6.ZeroIsValid = true;
            this.Column7.DataPropertyName = "Tien2_N3";
            style4.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column7.DefaultCellStyle = style4;
            this.Column7.HeaderText = "Vốn vay";
            this.Column7.Name = "Column7";
            this.Column7.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column7.UserTextNumericFormat = AppInstance.TextNumericFormat.LocalAmount;
            this.Column7.Width = 70;
            this.Column7.ZeroIsValid = true;
            this.Column8.DataPropertyName = "Tien2_N4";
            style5.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column8.DefaultCellStyle = style5;
            this.Column8.HeaderText = "Nguồn kh\x00e1c";
            this.Column8.Name = "Column8";
            this.Column8.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column8.UserTextNumericFormat = AppInstance.TextNumericFormat.LocalAmount;
            this.Column8.Width = 70;
            this.Column8.ZeroIsValid = true;
            this.Column9.DataPropertyName = "Tien2";
            style6.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column9.DefaultCellStyle = style6;
            this.Column9.HeaderText = "Tổng khấu hao";
            this.Column9.Name = "Column9";
            this.Column9.ReadOnly = true;
            this.Column9.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column9.UserTextNumericFormat = AppInstance.TextNumericFormat.LocalAmount;
            this.Column9.Width = 80;
            this.Column10.DataPropertyName = "Ngay_KH";
            this.Column10.HeaderText = "Ngày";
            this.Column10.Name = "Column10";
            this.Column10.ReadOnly = true;
            this.Column10.Resizable = DataGridViewTriState.False;
            this.Column10.Width = 0x3e;
            this.lblMonth.AutoSize = true;
            this.lblMonth.Font = new Font("Arial", 8.25f);
            this.lblMonth.Location = new Point(12, 0x1f);
            this.lblMonth.Name = "lblMonth";
            this.lblMonth.Size = new Size(0x68, 14);
            this.lblMonth.TabIndex = 1;
            this.lblMonth.Text = "Th\x00e1ng tính khấu hao";
            this.Toolbars.Items.AddRange(new ToolStripItem[] { this.txtSearch, this.tbrSep01, this.btnClose });
            this.Toolbars.Location = new Point(0, 0);
            this.Toolbars.Name = "Toolbars";
            this.Toolbars.Size = new Size(0x31a, 0x19);
            this.Toolbars.TabIndex = 2;
            this.Toolbars.Text = "Toolbars";
            this.txtSearch.Name = "txtSearch";
            this.txtSearch.Size = new Size(100, 0x19);
            this.txtSearch.Text = "T\x00ecm nhanh...";
            this.txtSearch.ToolTipText = "F4";
            this.tbrSep01.Name = "tbrSep01";
            this.tbrSep01.Size = new Size(6, 0x19);
            this.btnClose.DisplayStyle = ToolStripItemDisplayStyle.Image;
            this.btnClose.Image = Resources.CLOSE;
            this.btnClose.ImageTransparentColor = Color.Magenta;
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new Size(0x17, 0x16);
            this.btnClose.Text = "toolStripButton1";
            this.btnClose.ToolTipText = "Close (Esc)";
            this.btnClose.Click += new EventHandler(this.btnClose_Click);
            this.txtMonth.AllowNegative = false;
            this.txtMonth.Font = new Font("Arial", 8.25f);
            this.txtMonth.Location = new Point(0x7a, 0x1c);
            this.txtMonth.Name = "txtMonth";
            this.txtMonth.NumericPrecision = 2;
            this.txtMonth.NumericScaleOnFocus = 0;
            this.txtMonth.NumericScaleOnLostFocus = 0;
            int[] bits = new int[4];
            this.txtMonth.NumericValue = new decimal(bits);
            this.txtMonth.OldValue = null;
            this.txtMonth.Size = new Size(0x1d, 20);
            this.txtMonth.TabIndex = 0;
            this.txtMonth.TextAlign = HorizontalAlignment.Right;
//            this.txtMonth.Value = null;//manager.GetObject("txtMonth.Value");
            this.txtMonth.ZeroIsValid = false;
            this.txtYear.AllowNegative = false;
            this.txtYear.Font = new Font("Arial", 8.25f);
            this.txtYear.Location = new Point(0x9d, 0x1c);
            this.txtYear.Name = "txtYear";
            this.txtYear.NumericPrecision = 4;
            this.txtYear.NumericScaleOnFocus = 0;
            this.txtYear.NumericScaleOnLostFocus = 0;
            int[] numArray2 = new int[4];
            this.txtYear.NumericValue = new decimal(numArray2);
            this.txtYear.OldValue = null;
            this.txtYear.Size = new Size(60, 20);
            this.txtYear.TabIndex = 1;
            this.txtYear.TextAlign = HorizontalAlignment.Right;
//            this.txtYear.Value = null;//manager.GetObject("txtYear.Value");
            this.txtYear.ZeroIsValid = false;
            this.btnCalculate.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnCalculate.Font = new Font("Arial", 8.25f);
            this.btnCalculate.Location = new Point(0x139, 0x1b);
            this.btnCalculate.Name = "btnCalculate";
            this.btnCalculate.Size = new Size(0x59, 0x17);
            this.btnCalculate.TabIndex = 2;
            this.btnCalculate.Text = "T\x00ednh khấu hao";
            this.btnCalculate.UseVisualStyleBackColor = true;
            this.btnCalculate.Click += new EventHandler(this.btnCalculate_Click);
            this.btnDepreciateGet.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnDepreciateGet.Font = new Font("Arial", 8.25f);
            this.btnDepreciateGet.Location = new Point(0x198, 0x1b);
            this.btnDepreciateGet.Name = "btnDepreciateGet";
            this.btnDepreciateGet.Size = new Size(0x59, 0x17);
            this.btnDepreciateGet.TabIndex = 3;
            this.btnDepreciateGet.Text = "Lấy khấu hao";
            this.btnDepreciateGet.UseVisualStyleBackColor = true;
            this.btnDepreciateGet.Click += new EventHandler(this.btnDepreciateGet_Click);
            this.btnSave.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnSave.Font = new Font("Arial", 8.25f);
            this.btnSave.Location = new Point(0x1f7, 0x1b);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new Size(0x59, 0x17);
            this.btnSave.TabIndex = 4;
            this.btnSave.Text = "Lưu";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new EventHandler(this.btnSave_Click);
            this.btnCancel.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnCancel.Font = new Font("Arial", 8.25f);
            this.btnCancel.Location = new Point(0x256, 0x1b);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x59, 0x17);
            this.btnCancel.TabIndex = 5;
            this.btnCancel.Text = "Huỷ";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnMake.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnMake.Font = new Font("Arial", 8.25f);
            this.btnMake.Location = new Point(0x2b5, 0x1b);
            this.btnMake.Name = "btnMake";
            this.btnMake.Size = new Size(0x59, 0x17);
            this.btnMake.TabIndex = 6;
            this.btnMake.Text = "Lập chứng từ";
            this.btnMake.UseVisualStyleBackColor = true;
            this.btnMake.Click += new EventHandler(this.btnMake_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.ClientSize = new Size(0x31a, 0x214);
            base.Controls.Add(this.btnMake);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnSave);
            base.Controls.Add(this.btnDepreciateGet);
            base.Controls.Add(this.btnCalculate);
            base.Controls.Add(this.txtYear);
            base.Controls.Add(this.txtMonth);
            base.Controls.Add(this.Toolbars);
            base.Controls.Add(this.lblMonth);
            base.Controls.Add(this.grdControl);
            this.MinimumSize = new Size(0x322, 0x236);
            base.Name = "FormFACalculate";
            this.Text = "Chi tiết khấu hao tài sản, công cụ";
            ((ISupportInitialize) this.grdControl).EndInit();
            this.Toolbars.ResumeLayout(false);
            this.Toolbars.PerformLayout();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            Keys keys = keyData;
            if (keys != Keys.Escape)
            {
                if (keys == Keys.F4)
                {
                    this.txtSearch.Focus();
                    return true;
                }
                return base.ProcessCmdKey(ref msg, keyData);
            }
            base.Close();
            return true;
        }
    }
}

